﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReviewCurveApp.Classes;
using ReviewCurveApp.Contorller;

namespace ReviewCurveApp
{
    public partial class FrmUnitsView : Form
    {
        public FrmUnitsView()
        {
            InitializeComponent();
        }

        UnitsManager unitsManager = new UnitsManager();

        SubjectsManager subjectsManager = new SubjectsManager();

        private void button_LoadUnits_Click(object sender, EventArgs e)
        {
            LoadUnits();
        }

        private void LoadUnits()
        {
            ClearUnits();
            List<Unit> units = unitsManager.GetAllUnits();
            for (int i = 0; i < units.Count; i++)
            {
                Unit unit = units[i];
                checkedListBox1.Items.Add((i + 1).ToString() + "\t" + unit.Name, false);
            }
            checkedListBox1.Tag = units;
        }

        private void ClearUnits()
        {
            checkedListBox1.Items.Clear();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTasks(checkedListBox1.SelectedIndex);
        }

        private void LoadTasks(int index)
        {
            ClearTasks();
            List<Unit> units = checkedListBox1.Tag as List<Unit>;
            List<Task> tasks = units[index].Tasks;

            for (int i = 0; i < tasks.Count; i++)
            {
                Task task = tasks[i];
                listBox1.Items.Add((i + 1).ToString() + "\t" + task.ToString()+"\t"+task.ScheduledTime.ToString());
            }
            
        }

        private void ClearTasks()
        {

            listBox1.Items.Clear();


        }

        private void button_AddUnit_Click(object sender, EventArgs e)
        {
            FrmAddUnit frmAddUnit = new FrmAddUnit();
            if (frmAddUnit.ShowDialog() == DialogResult.OK)
            {
                LoadUnits();
            }
        }

        private void button_LoadSubjects_Click(object sender, EventArgs e)
        {
            LoadSubjects();
        }

        private void LoadSubjects()
        {
            ClearSubjects();

            List<Subject> subjects = subjectsManager.GetAllSubjects() ;

            for (int i = 0; i < subjects.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                
                Subject subject = subjects[i];
                item.Tag = subject;
                item.Text = subject.Id.ToString();
                item.SubItems.Add(subject.Name);
                item.SubItems.Add(subject.Description);
                listView1.Items.Add(item);
                
            }
            
        }

        private void ClearSubjects()
        {
            listView1.Items.Clear();
        }

        private void button_AddSubject_Click(object sender, EventArgs e)
        {
            FrmAddSubject frmAddSubject = new FrmAddSubject();
            if (frmAddSubject.ShowDialog() == DialogResult.OK)
            {
                LoadSubjects();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            Subject subject = listView1.SelectedItems[0].Tag as Subject;
            LoadUnits(subject);
        }

        private void LoadUnits(Subject subject)
        {
            ClearUnits();
            List<Unit> units = unitsManager.GetUnits(subject);
            for (int i = 0; i < units.Count; i++)
            {
                Unit unit = units[i];
                checkedListBox1.Items.Add((i + 1).ToString() + "\t" + unit.Name, false);
            }
            checkedListBox1.Tag = units;
        }
    }
}
